#include "userset.h"
#include "ui_userset.h"
#include "mainWidget.h"

UserSet::UserSet(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UserSet)
{
    ui->setupUi(this);

    pWidget = static_cast<Widget*>(parent);

    this->init();

    //返回设置子界面
    connect(ui->Btn_back, &QPushButton::clicked, this, [=](){
        pWidget->backSetPage();
    });
    //恢复出厂设置
    connect(ui->Btn_RST, &QPushButton::clicked, this, [=](){
        ui->stackedWidget->setCurrentIndex(UserSetDialog_id);
        ui->stackedWidget_dialog->setCurrentIndex(Rst_id);
    });
    connect(ui->Btn_RSTOk, &QPushButton::clicked, this, [=](){
        ui->stackedWidget->setCurrentIndex(UserSet_id);
    });
    connect(ui->Btn_RSTCancel, &QPushButton::clicked, this, [=](){
        ui->stackedWidget->setCurrentIndex(UserSet_id);
    });
    //时钟设置
    connect(ui->Btn_clockSet, &QPushButton::clicked, this, [=](){
        ui->stackedWidget->setCurrentIndex(TimeSet_id);
        this->editClr();
    });
    connect(ui->Btn_clockSetOk, &QPushButton::clicked, this, [=](){
        ui->stackedWidget->setCurrentIndex(UserSet_id);
        this->clockSetShow();
    });
    connect(ui->Btn_clockSetBack, &QPushButton::clicked, this, [=](){
        ui->stackedWidget->setCurrentIndex(UserSet_id);
        this->editClr();
    });
    //修改密码
    connect(ui->Btn_modPassword, &QPushButton::clicked, this, [=](){
        ui->stackedWidget->setCurrentIndex(ModPassword_id);
        this->editClr();
    });
    connect(ui->Btn_modPasswordOk, &QPushButton::clicked, this, [=](){
        if(this->passwordModErrorTip())
            ui->stackedWidget->setCurrentIndex(UserSet_id);
    });
    connect(ui->Btn_modPasswordBack, &QPushButton::clicked, this, [=](){
        ui->stackedWidget->setCurrentIndex(UserSet_id);
        this->editClr();
    });
    connect(ui->Btn_modPasswordCancel, &QPushButton::clicked, this, [=](){
        ui->stackedWidget->setCurrentIndex(ModPassword_id);
        this->editClr();
    });
    //加密设置
    connect(ui->Btn_encryptionSet, &QPushButton::clicked, this, [=](){
        ui->stackedWidget->setCurrentIndex(EncryptionSet_id);
    });
    connect(ui->Btn_encryptionSetOk, &QPushButton::clicked, this, [=](){
        ui->stackedWidget->setCurrentIndex(UserSet_id);
    });
    connect(ui->Btn_encryptionSetBack, &QPushButton::clicked, this, [=](){
        ui->stackedWidget->setCurrentIndex(UserSet_id);
    });
}

UserSet::~UserSet()
{
    delete ui;
}

void UserSet::init()
{
    ui->stackedWidget->setCurrentIndex(UserSet_id);
    Key = "123456";
    ui->Edit_password->setText(Key);
    //初始化显示系统开机时间
    QDateTime dateTime = QDateTime::currentDateTime();
    QString strTime = dateTime.toString("yyyy-MM-dd hh:mm:ss");
    ui->Btn_clockSet->setText(strTime);
}

//清空编辑框内容
void UserSet::editClr()
{
    ui->Edit_user->clear();
    ui->Edit_code->clear();
    ui->Edit_year->clear();
    ui->Edit_month->clear();
    ui->Edit_day->clear();
    ui->Edit_hour->clear();
    ui->Edit_minute->clear();
    ui->Edit_second->clear();
    ui->Edit_oldPassword->clear();
    ui->Edit_newPassword->clear();
    ui->Edit_renewPassword->clear();
    ui->Edit_admin->clear();
    ui->Edit_remainDays->clear();
    ui->Edit_mechCode->clear();
    ui->Edit_unlockCode->clear();
    ui->Edit_unlockPassword->clear();
}

//显示设置的时间
void UserSet::clockSetShow()
{
    int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
    year = ui->Edit_year->text().toInt();
    month = ui->Edit_month->text().toInt();
    day = ui->Edit_day->text().toInt();
    hour = ui->Edit_hour->text().toInt();
    minute = ui->Edit_minute->text().toInt();
    second = ui->Edit_second->text().toInt();
    char yearStr[5], monthStr[3], dayStr[3], hourStr[3], minuteStr[3], secondStr[3];
    sprintf(yearStr, "%d", year);
    sprintf(monthStr, "%d", month);
    sprintf(dayStr, "%d", day);
    sprintf(hourStr, "%d", hour);
    sprintf(minuteStr, "%d", minute);
    if (minuteStr[1] == '\0')      // 如果分为一位，如5，则需要转换字符串为两位，如05。
    {
        minuteStr[2] = '\0';
        minuteStr[1] = minuteStr[0];
        minuteStr[0] = '0';
    }
    sprintf(secondStr, "%d", second);
    if (secondStr[1] == '\0')        // 如果秒为一位，如5，则需要转换字符串为两位，如05。
    {
        secondStr[2] = '\0';
        secondStr[1] = secondStr[0];
        secondStr[0] = '0';
    }
    char s[21] = {0};      // 定义总日期时间char*变量。
    sprintf(s, "%s-%s-%s %s:%s:%s",
            yearStr, monthStr, dayStr, hourStr, minuteStr, secondStr);// 将年月日时分秒合并。
    QString timeShow = ((char*)s);
    ui->Btn_clockSet->setText(timeShow);
}

//密码更改错误提示，设置成功返回1，否则为0
bool UserSet::passwordModErrorTip()
{
    if(ui->Edit_oldPassword->text() != Key)
    {
        ui->stackedWidget->setCurrentIndex(UserSetDialog_id);
        ui->label_passwordErrorTip->setText("原密码错误，请输入正确原密码!");
        ui->label_passwordErrorTip->setAlignment(Qt::AlignCenter);
        ui->stackedWidget_dialog->setCurrentIndex(PasswordErrorTip_id);
        return 0;
    }
    else if(ui->Edit_newPassword->text().length() != 6)
    {
        ui->stackedWidget->setCurrentIndex(UserSetDialog_id);
        ui->label_passwordErrorTip->setText("请输入有效的六位密码!");
        ui->label_passwordErrorTip->setAlignment(Qt::AlignCenter);
        ui->stackedWidget_dialog->setCurrentIndex(PasswordErrorTip_id);
        return 0;
    }
    else if(ui->Edit_newPassword->text() != ui->Edit_renewPassword->text())
    {
        ui->stackedWidget->setCurrentIndex(UserSetDialog_id);
        ui->label_passwordErrorTip->setText("两次输入密码不一致!");
        ui->label_passwordErrorTip->setAlignment(Qt::AlignCenter);
        ui->stackedWidget_dialog->setCurrentIndex(-PasswordErrorTip_id);
        return 0;
    }
    else
    {
        Key = ui->Edit_newPassword->text();
        return 1;
    }
}
